<?php
/**
 * ***********************************************************************************
 * 咨询鲤 PC端知识店铺-API相关
 * 
 * @author Mr.RJH
 * @date 2025-09-16
 * @email<15213026209@163.com>
 * ***********************************************************************************
 */
namespace app\service;

use think\Request;
use think\exception\HttpResponseException;

class KnowStoreApiService
{ 
    /**
     * 商户ID
     * @var string
     */
    protected $m_id;

    /**
     * 用户user_token
     * @var string
     */
    protected $user_token;

    /**
     * 请求域名
     * @var string
     */
    protected $domain;

    /**
     * 渠道
     * @var string
     */
    protected $channel;

    /**
     * 平台端
     * @var string
     */
    protected $platform;

    /**
     * 构造方法
     * SystemLogService constructor.
     */
    public function __construct($m_id = '', $user_token = '', $channel = 'know_store', $platform = 'pc')
    {   
        $this->m_id = intval($m_id);
        $this->domain = 'https://api.zixunli.cn/';
        $this->channel = $channel;
        $this->platform = $platform;
        $this->user_token = $user_token;
        return $this;
    }

    /**
     * 创建商品分享海报
     * 
     * @param int $product_id 商品ID
     * @return string
     */
    public function createPoster($product_id)
    {
        $url = $this->domain.'product/createPoster';
        
        $arrCurlData = [];
        $arrCurlData['m_id'] = $this->m_id;
        $arrCurlData['product_id'] = $product_id;
        $arrCurlData = json_encode($arrCurlData, JSON_UNESCAPED_UNICODE);
        
        $arrCurlHeader = array(
            "Content-Type: application/json;charset=UTF-8",
            "authorization: ".$this->user_token."",
            "channel: ".$this->channel."",
            "platform: ".$this->platform."",
        );
        $retjson = http_post($url, $arrCurlData, $arrCurlHeader);
        $result = json_decode($retjson, true);
        if ($result['code'] == 200) {
            return $result['data'];
        } else {
            throw new \Exception($result['msg']);
        }
    }

    /**
     * 取消订单
     * 
     * @param int $order_id 订单ID
     * @param string $text 取消原因
     * @return array
     */
    public function cancelOrder($order_id, $text)
    {
        $url = $this->domain.'user/cancelOrder';
        
        $arrCurlData = [];
        $arrCurlData['m_id'] = $this->m_id;
        $arrCurlData['order_id'] = $order_id;
        $arrCurlData['text'] = $text;
        $arrCurlData = json_encode($arrCurlData, JSON_UNESCAPED_UNICODE);
        
        $arrCurlHeader = array(
            "Content-Type: application/json;charset=UTF-8",
            "authorization: ".$this->user_token."",
            "channel: ".$this->channel."",
            "platform: ".$this->platform."",
        );
        $retjson = http_post($url, $arrCurlData, $arrCurlHeader);
        $result = json_decode($retjson, true);
        if ($result['code'] == 200) {
            return true;
        } else {
            throw new \Exception($result['msg']);
        }
    }

    /**
     * 兑换码兑换
     * 
     * @param string $code 兑换码
     * @return string
     */
    public function markingCode($code)
    {
        $url = $this->domain.'user/markingCode';
        
        $arrCurlData = [];
        $arrCurlData['m_id'] = $this->m_id;
        $arrCurlData['code'] = $code;
        $arrCurlData = json_encode($arrCurlData, JSON_UNESCAPED_UNICODE);
        
        $arrCurlHeader = array(
            "Content-Type: application/json;charset=UTF-8",
            "authorization: ".$this->user_token."",
            "channel: ".$this->channel."",
            "platform: ".$this->platform."",
        );
        $retjson = http_post($url, $arrCurlData, $arrCurlHeader);
        $result = json_decode($retjson, true);
        if ($result['code'] == 200) {
            return true;
        } else {
            throw new \Exception($result['msg']);
        }
    }

    /**
     * 获取题库章节
     * 
     * @return array
     */
    public function chapters($product_id, $version, $pid = 0, $page = '', $limit = '')
    {
        $url = $this->domain.'question/chapters';
        
        $arrCurlData = [];
        $arrCurlData['m_id'] = $this->m_id;
        $arrCurlData['product_id'] = $product_id;
        $arrCurlData['page'] = $page;
        $arrCurlData['limit'] = $limit;
        $arrCurlData['version'] = $version;
        $arrCurlData = json_encode($arrCurlData, JSON_UNESCAPED_UNICODE);
        
        $arrCurlHeader = array(
            "Content-Type: application/json;charset=UTF-8",
            "authorization: ".$this->user_token."",
            "channel: ".$this->channel."",
            "platform: ".$this->platform."",
        );
        $retjson = http_post($url, $arrCurlData, $arrCurlHeader);
        $result = json_decode($retjson, true);

        if ($result['code'] == 200) {
            return $result['data'];
        } else {
            throw new \Exception($result['msg']);
        }
    }

    /**
     * 预下单算价
     * 
     * @return array
     */
    public function calcPrice($attr_id, $product_id, $goods_num = 1, $coupon_id = 0, $address_id = 0)
    {
        $url = $this->domain.'order/calcPrice';
        
        $arrCurlData = [];
        $arrCurlData['m_id'] = $this->m_id;
        $arrCurlData['attr_id'] = $attr_id;
        $arrCurlData['product_id'] = $product_id;
        $arrCurlData['action'] = 'calc';
        $arrCurlData['number'] = $goods_num;
        $arrCurlData['coupon_id'] = $coupon_id;
        $arrCurlData['address_id'] = $address_id;
        $arrCurlData = json_encode($arrCurlData, JSON_UNESCAPED_UNICODE);
        $arrCurlHeader = array(
            "Content-Type: application/json;charset=UTF-8",
            "authorization: ".$this->user_token."",
            "channel: ".$this->channel."",
            "platform: ".$this->platform."",
        );
        $retjson = http_post($url, $arrCurlData, $arrCurlHeader);
        $result = json_decode($retjson, true);
        if ($result['code'] == 200) {
            return $result['data'];
        } else {
            if (Request()->isGet()) {
                throw new HttpResponseException(redirect('/error/http404'));
            }
            throw new \Exception($result['msg']);
        }
    }

    /**
     * 创建订单
     * 
     * @param array $order 订单数据
     * @param string $type cart：购物车 normal：普通单次下单
     * @return bool
     */
    public function orderCreate($order, $type = 'normal')
    {
        $url = $this->domain.'order/create';
        
        $arrCurlData = [];
        $arrCurlData['m_id'] = $this->m_id;
        $arrCurlData['attr_id'] = $order['attr_id'];
        $arrCurlData['product_id'] = $order['product_id'];
        $arrCurlData['pay_type'] = $order['pay_type'];
        $arrCurlData['number'] = $order['num'];
        $arrCurlData['customer_notes'] = $order['customer_notes'];
        $arrCurlData['coupon_id'] = $order['coupon_id'] ?? 0;
        if (isset($order['address_id'])) {
            $arrCurlData['address_id'] = $order['address_id'];
        }
        $arrCurlData['type'] = $type;
        $arrCurlData = json_encode($arrCurlData, JSON_UNESCAPED_UNICODE);
        
        $arrCurlHeader = array(
            "Content-Type: application/json;charset=UTF-8",
            "authorization: ".$this->user_token."",
            "channel: ".$this->channel."",
            "platform: ".$this->platform."",
        );
        $retjson = http_post($url, $arrCurlData, $arrCurlHeader);
        $result = json_decode($retjson, true);
        if ($result['status'] == 200) {
            return $result['data'];
        } else {
            throw new \Exception($result['msg']);
        }
    }

    /**
     * 再次支付
     * 
     * @param array $order 订单信息
     * @return array
     */
    public function payAgainOrder($order)
    {
        $url = $this->domain.'order/payAgainOrder';
        
        $arrCurlData = [];
        $arrCurlData['m_id'] = $this->m_id;
        $arrCurlData['orderid'] = $order['order_id'];
        $arrCurlData['pay_type'] = $order['pay_type'];
        $arrCurlData = json_encode($arrCurlData, JSON_UNESCAPED_UNICODE);
        
        $arrCurlHeader = array(
            "Content-Type: application/json;charset=UTF-8",
            "authorization: ".$this->user_token."",
            "channel: ".$this->channel."",
            "platform: ".$this->platform."",
        );
        $retjson = http_post($url, $arrCurlData, $arrCurlHeader);
        $result = json_decode($retjson, true);
        
        if ($result['status'] == 200) {
            return $result['data'];
        } else {
            throw new \Exception($result['msg']);
        }
    }

    /**
     * 免费商品领取
     * 
     * @return bool
     */
    public function getFreePower($product_id)
    {
        $url = $this->domain.'product/getFreePower';
        
        $arrCurlData = [];
        $arrCurlData['m_id'] = $this->m_id;
        $arrCurlData['product_id'] = $product_id;
        $arrCurlData = json_encode($arrCurlData, JSON_UNESCAPED_UNICODE);
        
        $arrCurlHeader = array(
            "Content-Type: application/json;charset=UTF-8",
            "authorization: ".$this->user_token."",
            "channel: ".$this->channel."",
            "platform: ".$this->platform."",
        );
        $retjson = http_post($url, $arrCurlData, $arrCurlHeader);
        $result = json_decode($retjson, true);
        
        if ($result['code'] == 200) {
            return true;
        } else {
            throw new \Exception($result['msg']);
        }
    }
}